package util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Date;
import java.util.Random;

public class EncryptPass
{

    private static Object        _lock;
    private static MessageDigest _md5;

    public static void main(String[] args) throws Exception {

        String pass = "Ankara03";
        String encPass = getEncryptedPass(pass, "CBHex");
        System.out.println("encpass :" + encPass );
    }

    private static byte[] md5(String s) throws Exception {

        byte[] digesteds = (byte[]) null;
        _md5 = MessageDigest.getInstance("MD5");
        _lock = new Object();
        synchronized (_lock) {
            _md5.update(s.getBytes());
            digesteds = _md5.digest();
            _md5.reset();
        }
        return digesteds;
    }

    public static String getEncryptedPass(String plainPass, String encMethod) throws Exception {

        String retStr;
        if (encMethod.equals("CBHex")) {
            retStr = toString(md5(plainPass));
        } else {
            String e = null;
            String s = null;
            Random rand = new Random();
            try {
                Thread.currentThread();
                Thread.sleep(100L);
            } catch (Exception localException) {
            }
            rand.setSeed(new Date().getTime());
            s = String.valueOf(rand.nextInt());
            if (s.length() < 8) {
                int k = s.length();
                for (int j = 0; j < 8 - k; j++) {
                    s = s + "1";
                }
            }
            s = s.substring(s.length() - 8);
            e = new String(new BigInteger(plainPass.getBytes()).xor(new BigInteger(s.getBytes())).toByteArray());
            retStr = toString( (s.substring(0, 4) + e + s.substring(4)).getBytes());
        }
        return retStr;
    }

    public static String toString(byte[] abyte0, int i, int j, boolean flag) {

        String s = flag ? "0123456789ABCDEF" : "0123456789abcdef";
        StringBuffer stringbuffer = new StringBuffer(j * 2);
        for (int k = i + j; i < k; i++) {
            stringbuffer.append(s.charAt( (abyte0[i] & 0xF0) >>> 4));
            stringbuffer.append(s.charAt(abyte0[i] & 0xF));
        }
        return stringbuffer.toString();
    }

    public static String toString(byte[] abyte0) {

        return toString(abyte0, 0, abyte0.length, true);
    }

    public static byte[] toByteArray(String s) {

        return toByteArray(s, 0, s.length());
    }

    public static byte[] toByteArray(String s, int i, int j) {

        if (j % 2 != 0)
            throw new IllegalArgumentException("Illegal length of Hex encoding: " + j + " (not n*2)");
        if (j == 0)
            return new byte[0];
        byte[] abyte0 = new byte[j / 2];
        int k = 0;
        int l = 0;
        for (int i1 = i + j; i < i1;) {
            int j1 = Character.digit(s.charAt(i), 16);
            if (j1 < 0)
                throw new IllegalArgumentException("Illegal characters in Hex encoding: " + s.charAt(i));
            k = (k << 4) + j1;
            if (l % 2 == 1)
                abyte0[ (l / 2)] = (byte) k;
            i++;
            l++;
        }
        return abyte0;
    }

    public static String toString(byte[] abyte0, int i, int j) {

        return toString(abyte0, i, j, true);
    }
}
